package com.web.servlet;

import com.bean.Disaster;
import com.dao.UserDao;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.web.common.exception.DataAccessException;
import com.web.common.utils.BeanFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author wangzhipeng
 * @Date 2020-09-01 11:35 上午
 * @Description
 */
@WebServlet(urlPatterns = "/GetMyUploadServlet")
public class GetMyUploadServlet extends HttpServlet {
    UserDao userDao = (UserDao) BeanFactory.getBean(BeanFactory.USER_DAO);

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("正在获取个人上传信息");
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        request.setCharacterEncoding("UTF-8");
        List<Disaster> list = null;
        try {
            list = userDao.selectMyDisaster(uid);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter printWriter = response.getWriter();
        ObjectMapper objectMapper = new ObjectMapper();
        String result = objectMapper.writeValueAsString(list);
        System.out.println(result);
        printWriter.println(result);
        printWriter.flush();
        printWriter.close();
    }
}
